﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace WebClientTest
{
  public class CookieAwareWebClient : WebClient
  {
		#region Fields (1) 

    private CookieContainer m_container = new CookieContainer();
    private Uri m_uri;

		#endregion Fields 

    public CookieAwareWebClient(Uri uri)
      : base()
    {
      m_uri = uri;
    }

		#region Methods (2) 

		// Public Methods (1) 

    public void AddCookie(string key, string value)
    {
      m_container.Add(m_uri, new Cookie(key, value));
    }
		// Protected Methods (1) 

    protected override WebRequest GetWebRequest(Uri address)
    {
      WebRequest request = base.GetWebRequest(address);
      HttpWebRequest webRequest = request as HttpWebRequest;
      if (webRequest != null)
      {
        webRequest.CookieContainer = m_container;
      }
      return request;
    }

		#endregion Methods 
  }
}
